package com.hongxinguoji.manager.admin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongxinguoji.manager.admin.pojo.HxAuthRole;
import com.hongxinguoji.manager.admin.service.IRoleService;
import com.hongxinguoji.manager.common.util.EasyUIDataGridResult;
import com.hongxinguoji.manager.common.util.HXResult;
import com.hongxinguoji.manager.common.util.IDUtils;

@Controller
@RequestMapping("/admin")
public class RoleController {

	@Autowired
	private IRoleService roleService;


	@RequestMapping("/rolelist")
	@ResponseBody
	public EasyUIDataGridResult getRoleList(Integer page, Integer rows) {
		EasyUIDataGridResult result = new EasyUIDataGridResult();

		// 分页处理
		PageHelper.startPage(page, rows);
		List<HxAuthRole> list = roleService.getRoleList();
		PageInfo<HxAuthRole> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		result.setRows(list);
		return result;

	}

	@RequestMapping("/roleAdd")
	@ResponseBody
	public HXResult addRole(String ids, HxAuthRole role) {
		try {
			String stringId = IDUtils.getNumStringId();
			role.setId(stringId);
			roleService.addRole(ids, role);
			return HXResult.ok("添加角色成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "添加角色失败！");
		}
	}

	@RequestMapping("/getallRoles")
	@ResponseBody
	public List<HxAuthRole> getallRoles() {
		return roleService.getallRoles();
	}

	/**
	 * 删除角色
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/roledelete")
	@ResponseBody
	public HXResult deleteRole(String ids) {
		try {

			roleService.deleRole(ids);
			return HXResult.ok("角色删除成功！");
		} catch (Exception e) {
			// 打印错误信息
			e.printStackTrace();
			// 返回删除失败
			return HXResult.ok("角色删除失败！");
		}
	}

	@RequestMapping("/role111")
	@ResponseBody
	public HXResult updateRole(String ids, HxAuthRole role) {
		// 获取角色id
		String id = role.getId();
		try {
			// 删除外键关系
			roleService.deleRole(id);
			// 保存角色数据
			roleService.addRole(ids, role);
			return HXResult.ok("角色修改成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "角色修改失败！");
		}
	}

}
